import { IDispose } from "../../gengine/core/IDispose";
import { UICompomentPool } from "../utils/UICompomentPool";
//class ScaleBitmap
    
    export  class ScaleBitmap /* flash.display.Bitmap */ implements IDispose
    {
        constructor(arg1: Object /* flash.display.BitmapData */=null, arg2: string="auto", arg3: Boolean=false, arg4: Boolean=false)
        {
            super(this._originalBitmap, arg2, arg3);
            this._originalBitmap = arg1;
            this._keepTexture = arg4;
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStage);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }

        public clear(): void
        {
            if (this._createBitmap) 
            {
                this._isCleared = true;
                this._createBitmap.dispose();
                this._createBitmap = null;
            }
            return;
        }

        public restore(): void
        {
            var loc1=NaN;
            var loc2=NaN;
            if (this._isCleared) 
            {
                loc1 = this.scaleX;
                loc2 = this.scaleY;
                this.scaleX = 1;
                this.scaleY = 1;
                this.setSize(this._width, this._height);
                this.scaleX = loc1;
                this.scaleY = loc2;
            }
            return;
        }

        public createDisposeChildren(): void
        {
            if (this._isDisposed) 
            {
                this._isDisposed = false;
            }
            return;
        }

        /* internal  */onAddedToStage(arg1: Object /* flash.events.Event */): void
        {
            this._isOnStage = true;
            this.restore();
            return;
        }

        /* internal  */onRemoveFromStage(arg1: Object /* flash.events.Event */): void
        {
            this._isOnStage = false;
            this.clear();
            return;
        }

        public /* override */ set bitmapData(arg1: Object /* flash.display.BitmapData */)
        {
            this._originalBitmap = arg1;
            this._width = arg1.width;
            this._height = arg1.height;
            if (this._scale9Grid == null) 
            {
                this.assignBitmapData(this._originalBitmap);
            }
            else 
            {
                if (!this.validGrid(this._scale9Grid)) 
                {
                    this._scale9Grid = null;
                }
                this.setSize(arg1.width, arg1.height);
            }
            return;
        }

        public /* override */ set width(arg1: number)
        {
            if (arg1 != width) 
            {
                this.setSize(arg1, height);
            }
            return;
        }

        public /* override */ set height(arg1: number)
        {
            if (arg1 != height) 
            {
                this.setSize(width, arg1);
            }
            return;
        }

        public /* override */ set scale9Grid(arg1: Object /* flash.geom.Rectangle */)
        {
            var loc1=NaN;
            var loc2=NaN;
            if (this._scale9Grid == null && !(arg1 == null) || !(this._scale9Grid == null) && (arg1 == null || !this._scale9Grid.equals(arg1))) 
            {
                if (arg1 != null) 
                {
                    if (!this.validGrid(arg1)) 
                    {
                        throw new Error("#001 - The _scale9Grid does not match the original BitmapData");
                    }
                    this._scale9Grid = arg1.clone();
                    this.resizeBitmap(width, height);
                    scaleX = 1;
                    scaleY = 1;
                }
                else 
                {
                    loc1 = width;
                    loc2 = height;
                    this._scale9Grid = null;
                    this.assignBitmapData(this._originalBitmap);
                    this.setSize(loc1, loc2);
                }
            }
            return;
        }

        protected assignBitmapData(arg1: Object /* flash.display.BitmapData */): void
        {
            super.bitmapData = arg1;
            return;
        }

        /* internal  */validGrid(arg1: Object /* flash.geom.Rectangle */): Boolean
        {
            return arg1.right <= this._originalBitmap.width && arg1.bottom <= this._originalBitmap.height;
        }

        public /* override */ get scale9Grid(): Object /* flash.geom.Rectangle */
        {
            return this._scale9Grid;
        }

        public setSize(arg1: number, arg2: number): void
        {
            if (this._scale9Grid != null) 
            {
                if (this._originalBitmap) 
                {
                    arg1 = Math.max(arg1, this._originalBitmap.width - this._scale9Grid.width);
                    arg2 = Math.max(arg2, this._originalBitmap.height - this._scale9Grid.height);
                }
                this.resizeBitmap(arg1, arg2);
            }
            else 
            {
                super.width = arg1;
                super.height = arg2;
            }
            this._width = arg1;
            this._height = arg2;
            return;
        }

        public getOriginalBitmapData(): Object /* flash.display.BitmapData */
        {
            return this._originalBitmap;
        }

        protected resizeBitmap(arg1: number, arg2: number): void
        {
            var loc5=null;
            var loc6=null;
            var loc8=0;
            var loc9=0;
            var loc10=null;
            var loc11=null;
            var loc12=null;
            if (!(arg1 > 0 && arg2 > 0)) 
            {
                return;
            }
            if (!this._isOnStage) 
            {
                if (!ScaleBitmap.defaultBitmapData) 
                {
                    ScaleBitmap.defaultBitmapData = new Object /* flash.display.BitmapData */(1, 1);
                }
                this.assignBitmapData(ScaleBitmap.defaultBitmapData);
            }
            if (this._createBitmap) 
            {
                this._createBitmap.dispose();
            }
            this._createBitmap = new Object /* flash.display.BitmapData */(arg1, arg2, true, 0);
            this._isCleared = false;
            var loc1=[0, this._scale9Grid.top, this._scale9Grid.bottom, this._originalBitmap.height];
            var loc2=[0, this._scale9Grid.left, this._scale9Grid.right, this._originalBitmap.width];
            var loc3=[0, this._scale9Grid.top, arg2 - (this._originalBitmap.height - this._scale9Grid.bottom), arg2];
            var loc4=[0, this._scale9Grid.left, arg1 - (this._originalBitmap.width - this._scale9Grid.right), arg1];
            var loc7=new Object /* flash.geom.Matrix */();
            if (this._keepTexture) 
            {
                loc10 = new Object /* flash.display.Shape */();
                loc8 = 0;
                while (loc8 < 3) 
                {
                    loc9 = 0;
                    while (loc9 < 3) 
                    {
                        loc6 = new Object /* flash.geom.Rectangle */(loc2[loc8], loc1[loc9], loc2[loc8 + 1] - loc2[loc8], loc1[loc9 + 1] - loc1[loc9]);
                        (loc11 = new Object /* flash.display.BitmapData */(loc2[loc8 + 1] - loc2[loc8], loc1[loc9 + 1] - loc1[loc9], true, 0)).copyPixels(this._originalBitmap, loc6, new Object /* flash.geom.Point */(0, 0));
                        loc10.graphics.clear();
                        loc10.graphics.beginBitmapFill(loc11);
                        loc10.graphics.drawRect(0, 0, loc4[loc8 + 1] - loc4[loc8], loc3[loc9 + 1] - loc3[loc9]);
                        loc12 = new Object /* flash.display.BitmapData */(loc4[loc8 + 1] - loc4[loc8], loc3[loc9 + 1] - loc3[loc9], true, 0);
                        loc6 = new Object /* flash.geom.Rectangle */(0, 0, loc4[loc8 + 1] - loc4[loc8], loc3[loc9 + 1] - loc3[loc9]);
                        loc12.draw(loc10);
                        this._createBitmap.copyPixels(loc12, loc6, new Object /* flash.geom.Point */(loc4[loc8], loc3[loc9]));
                        loc11.dispose();
                        loc11 = null;
                        loc12.dispose();
                        ++loc9;
                    }
                    ++loc8;
                }
                this.assignBitmapData(this._createBitmap);
            }
            else 
            {
                loc8 = 0;
                while (loc8 < 3) 
                {
                    loc9 = 0;
                    while (loc9 < 3) 
                    {
                        loc5 = new Object /* flash.geom.Rectangle */(loc2[loc8], loc1[loc9], loc2[loc8 + 1] - loc2[loc8], loc1[loc9 + 1] - loc1[loc9]);
                        loc6 = new Object /* flash.geom.Rectangle */(loc4[loc8], loc3[loc9], loc4[loc8 + 1] - loc4[loc8], loc3[loc9 + 1] - loc3[loc9]);
                        loc7.identity();
                        loc7.a = loc6.width / loc5.width;
                        loc7.d = loc6.height / loc5.height;
                        loc7.tx = loc6.x - loc5.x * loc7.a;
                        loc7.ty = loc6.y - loc5.y * loc7.d;
                        this._createBitmap.draw(this._originalBitmap, loc7, null, null, loc6, smoothing);
                        ++loc9;
                    }
                    ++loc8;
                }
                this.assignBitmapData(this._createBitmap);
            }
            return;
        }

        public dispose(arg1: Boolean=true): void
        {
            if (this._isDisposed) 
            {
                return;
            }
            if (this.parent) 
            {
                this.parent.removeChild(this);
            }
            this.clear();
            super.bitmapData = null;
            this._isDisposed = true;
            this._scale9Grid = null;
            this._isCleared = true;
            this._isOnStage = false;
            super.x = 0;
            super.y = 0;
            if (arg1) 
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }

        protected /* var */_isCleared: Boolean=true;

        protected /* var */_isOnStage: Boolean=false;

        protected /* var */_originalBitmap: Object /* flash.display.BitmapData */;

        protected /* var */_createBitmap: Object /* flash.display.BitmapData */;

        protected /* var */_scale9Grid: Object /* flash.geom.Rectangle */=null;

        protected /* var */_keepTexture: Boolean;

        protected /* var */_isDisposed: Boolean=true;

        protected /* var */_width: number=-1;

        protected /* var */_height: number=-1;

        private static /* var */defaultBitmapData: Object /* flash.display.BitmapData */;
    }
